package com.hfy.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hfy.model.dto.UpdateDto;
import com.hfy.model.po.user.FollowPo;
import com.hfy.model.po.user.UserCommentPo;
import com.hfy.model.vo.UserInfoVo;
import com.hfy.model.vo.user.MineInfoVo;
import com.hfy.user.mapper.InfoMapper;
import com.hfy.user.service.FollowService;
import com.hfy.user.service.InfoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import result.Result;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static constants.OSSConstants.*;
import static constants.RedisConstants.LOGIN_TOKEN_KEY;

/**
 * @ author 韩
 * time: 2024/5/20 2:15
 */
@Service
public class InfoServiceImpl implements InfoService {

//    @Autowired
//    private KafkaTemplate<String,String> kafkaTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private InfoMapper infoMapper;

    @Autowired
    private FollowService followService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 验证密码长度的方法
    private boolean isValidPassword(String password) {
        // 密码长度范围为3~15个字符
        Pattern pattern = Pattern.compile("^\\S{3,15}$");
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

    // 验证用户名长度的方法
    private boolean isValidUsername(String username) {
        // 用户名长度范围为3~15个字符
        Pattern pattern = Pattern.compile("^\\S{3,15}$");
        Matcher matcher = pattern.matcher(username);
        return matcher.matches();
    }

    @Override
    public Result updateInfo(MultipartFile avatar, UpdateDto dto) {
        // 验证密码长度
        if (!isValidPassword(dto.getPassword())) {
            return Result.error("昵称或密码必须由3~15字符组成！");
        }
        // 验证昵称长度
        if (!isValidUsername(dto.getUsername())) {
            return Result.error("昵称或密码必须由3~15个字符组成！");
        }
        // 去根据账号修改信息
        String avatarName = OSS_COVER_PREFIX + dto.getAccount() + OSS_COVER_SUFFIX;
        String avatarSqlName =  OSS_HTTPS_PREFIX + OSS_BUCKET_NAME + "."  + OSS_ENDPOINT + "/" + OSS_AVATAR_USER_PREFIX + dto.getAccount() + OSS_AVATAR_SUFFIX;
        dto.setAvatar(avatarSqlName);
        infoMapper.updateByAccount(dto);
        // 在前端修改成功 就修改本地内存
        return Result.ok("账号信息修改成功！！！");
    }

    @Override
    public Result getInfo(String token) {
        // 从redis中获取到对应账户的个人信息
        String tokenKey = LOGIN_TOKEN_KEY + token;
        Long account = Long.parseLong((String) Objects.requireNonNull(redisTemplate.opsForHash().get(tokenKey, "account")));
        String username = (String)redisTemplate.opsForHash().get(tokenKey, "username");
        String avatar = (String)redisTemplate.opsForHash().get(tokenKey, "avatar");

        UserInfoVo vo = new UserInfoVo();
        vo.setAccount(account);
        vo.setUsername(username);
        vo.setAvatar(avatar);
        return Result.ok(vo);
    }

    @Override
    public UserCommentPo getInfoByAccount(Long account) {
        // 只需要username和avatar

        return infoMapper.getInfoByAccount(account);
    }

    @Override
    public Result getPageInfoByAccount(Long account, Long mineAccount) {

        // 数据经过校验都是正确的，直接进行逻辑
        UserCommentPo userInfo = infoMapper.getInfoByAccount(account);
        // 获取不到直接返回空，前端会根据这个null做处理
        if (userInfo == null) return Result.ok(null ,"找不到该用户");
        // 然后是粉丝量查询
        LambdaQueryWrapper<FollowPo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(FollowPo::getIdolAccount, account).eq(FollowPo::getIsDie, 0);
        long fansNum = followService.count(queryWrapper1);
        // 然后是关注数查询
        LambdaQueryWrapper<FollowPo> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(FollowPo::getFansAccount, account).eq(FollowPo::getIsDie, 0);;
        long followNum = followService.count(queryWrapper2);
        // 最后是查询关系，我是否关注了这个账号
        boolean isFollow = false;
        LambdaQueryWrapper<FollowPo> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(FollowPo::getIdolAccount, account).eq(FollowPo::getFansAccount, mineAccount).eq(FollowPo::getIsDie, 0);;
        FollowPo one = followService.getOne(queryWrapper3);
        if (one != null) {
            isFollow = true;
        }
        MineInfoVo mineInfoVo = new MineInfoVo();
        mineInfoVo.setAvatar(userInfo.getAvatar());
        mineInfoVo.setUsername(userInfo.getUsername());
        mineInfoVo.setFansNum(fansNum);
        mineInfoVo.setFollowNum(followNum);
        mineInfoVo.setIsFollow(isFollow);

        return Result.ok(mineInfoVo);
    }
}
